package com.mhc.suzuki.util;

import com.mhc.suzuki.constant.ErrorCodeEnum;
import com.mhc.suzuki.exception.SuzukiCommonException;
import com.mhc.suzuki.vo.DateRangeVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * 日期操作
 */
@Slf4j
public class DateUtil {

    private DateUtil() {
    }

    /**
     * 得到指定格式的字符串日期
     *
     * @param format 格式
     * @return 格式化后的日期字符串
     */
    public static String getNowTime(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new GregorianCalendar().getTime());
    }

    public static String parseDateToString(LocalDateTime localDateTime, String format) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(dtf);
    }

    public static String defaultParseDateToString(LocalDateTime localDateTime) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-hh");
        return localDateTime.format(dtf);
    }

    public static LocalDateTime defaultParseStringToDateTime(String localDateTime) {
        if(StringUtils.isBlank(localDateTime)){
            return null;
        }
        try{
            return LocalDateTime.parse(localDateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }catch (DateTimeParseException e){
            try {
                return LocalDateTime.parse(localDateTime+" 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            }catch (DateTimeParseException ex){
                log.error("localDateTime:{}",localDateTime);
                throw new SuzukiCommonException(ErrorCodeEnum.DATEFORMAT_ERROR);
            }
        }
    }


    /**
     * 获取上个月的时间范围
     * @return
     */
    public static DateRangeVO getLastMonth(){
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.add(Calendar.MONTH, -1);
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(startCalendar);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.add(Calendar.MONTH, -1);
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(endCalendar);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        return new DateRangeVO(sdf.format(startCalendar.getTime()), sdf.format(endCalendar.getTime()));
    }

    private static void setMinTime(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    private static void setMaxTime(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
    }


}